using LCL;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace LCL.Tools
{
    /// <summary>
    /// 为指定实体模型生成对应的属性文档对象列表
    /// </summary>
    public class PropertyDocParser
    {
        public string Comment { get; set; }
        public string ClassContent { get; set; }
        private List<PropertyDoc> _result = new List<PropertyDoc>();
        public IList<PropertyDoc> Parse()
        {
            if (string.IsNullOrWhiteSpace(this.ClassContent)) throw new ArgumentNullException("this.ClassContent");
            this.ParseCommentsFromContent();
            this.ParseCommentsFromPartialContent();
            //this.ParseDoc();
            this.Comment = this.GetComment("class");

            #region MyRegion
            this._result.Add(new PropertyDoc
            {
                PropertyName = "ID",
                Comment = "编号",
            });
            this._result.Add(new PropertyDoc
            {
                PropertyName = "AddDate",
                Comment = "添加时间",
            });
            this._result.Add(new PropertyDoc
            {
                PropertyName = "UpdateDate",
                Comment = "更新时间",
            });
            this._result.Add(new PropertyDoc
            {
                PropertyName = "IsDelete",
                Comment = "删除标记",
            });
            #endregion
            return this._result;
        }
        #region 解析注释
        private void ParseCommentsFromContent()
        {
            var matches = Regex.Matches(this.ClassContent, @"<summary\>(?<content>.+?)\</summary\>.+?public partial \S+ (?<name>\S+)\s",
                RegexOptions.Singleline);
            foreach (Match match in matches)
            {
                var name = match.Groups["name"].Value;
                var comment = match.Groups["content"].Value;
                comment = Regex.Replace(comment, @" +/// ", string.Empty).Trim();
                comment = Regex.Replace(comment, "///", string.Empty).Trim();

                this._result.Add(new PropertyDoc
                {
                    PropertyName = name,
                    Comment = comment,
                });

            }
        }
        private void ParseCommentsFromPartialContent()
        {
            var matches = Regex.Matches(this.ClassContent, @"<summary\>(?<content>.+?)\</summary\>.+?public \S+ (?<name>\S+)\s",
                RegexOptions.Singleline);
            foreach (Match match in matches)
            {
                var name = match.Groups["name"].Value;
                var comment = match.Groups["content"].Value;
                comment = Regex.Replace(comment, @" +/// ", string.Empty).Trim();
                comment = Regex.Replace(comment, "///", string.Empty).Trim();

                this._result.Add(new PropertyDoc
                {
                    PropertyName = name,
                    Comment = comment,
                });
            }
        }
        private string GetComment(string property)
        {
            PropertyDoc propertyDoc = this._result.SingleOrDefault(p => p.PropertyName == property);
            if (propertyDoc != null)
            {
                return propertyDoc.Comment;
            }
            return "";
        }
        #endregion
    }
}